﻿#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include <reent.h>

/* newlib nano provides a stub version of "_write_r()" that does nothing.  The stub has a
 * weak binding, so defining the function in your own files will override the stub.
 */
_ssize_t _write_r(struct _reent *ptr, /* Don't worry about what's in this for the simple case */
				  int fd,			  /* ignored */
				  const void *buf,	  /* the data to be sent out the UART */
				  size_t cnt)		  /* the number of bytes to be sent */
{
	/* Replace "huart3" with the pointer to the UART or USART instance you are using
	 * in your project
	 */
	// HAL_UART_Transmit(&huart3, (uint8_t *)buf, cnt, 1000);

	for (int i = 0; i < cnt; i++)
	{
		while ((USART1->SR & 0X40) == 0)
			; //循环发送,直到发送完毕
		USART1->DR = ((u8 *)buf)[i];
	}

	return (_ssize_t)cnt;
}

// LED灯闪--- IO测试

int main(void)
{
	delay_init();	   //延时函数初始化
	LED_GPIO_Config(); // LED 端口初始化
	uart_init(115200); // 初始化串口
	char *hi = "hello world\n";
	while (1)
	{
		printf("%s   %s\n", hi, hi);
		delay_ms(1000); //延时300ms
	}

	// printf(); //通过串口输出信息

	while (1)
	{
		LED_D2(ON);		// 亮
		delay_ms(1000); //延时300ms
		LED_D2(OFF);	// 灭
		delay_ms(1000); //延时300ms
	}
}
